package dao;

import pojo.Flight;
import pojo.User;
import util.DBConnectionUtil;
import util.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    public User getUser(String userName, String password) {
        PreparedStatement pstmt = null;
        ResultSet resultSet = null;
        User user =  null;
        // 创建数据库连接
        Connection conn = JdbcUtils.getConnection();
        // 插入数据库信息
        String sql = "select username from user where username=? and password=?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, userName);
            pstmt.setString(2,password);
            resultSet = pstmt.executeQuery();
            while(resultSet.next()){
                user = new User();
                user.setUsername(resultSet.getString("username"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(resultSet,pstmt, conn);
            return user;
        }
    }

    // 新增的 addUser 方法
    public boolean addUser(User user) {
        PreparedStatement pstmt = null;
        ResultSet resultSet = null;
        boolean result = false;
        // 创建数据库连接
        Connection conn = JdbcUtils.getConnection();
        // 插入数据库信息的SQL语句
        String sql = "insert into user (username, password) values (?, ?)";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getUsername());
            pstmt.setString(2, user.getPassword());
            int affectedRows = pstmt.executeUpdate();
            // 如果受影响的行数为1，则表示插入成功
            if (affectedRows == 1) {
                result = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(resultSet,pstmt, conn);
        }
        return result;
    }

    public User getUserByUsernameAndPassword(String username, String oldPassword) {
        PreparedStatement pstmt = null;
        ResultSet resultSet = null;
        User user = null;
        // 创建数据库连接
        Connection conn = JdbcUtils.getConnection();
        // 查询数据库信息的SQL语句
        String sql = "SELECT * FROM user WHERE username = ? AND password = ?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username);
            pstmt.setString(2, oldPassword); // 注意：在实际应用中，密码应该是加密存储和比较的
            resultSet = pstmt.executeQuery();
            // 如果结果集不为空且至少有一行数据，则获取用户信息
            if (resultSet.next()) {
                user = new User();
                user.setId(resultSet.getString("id"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setGender(resultSet.getString("gender"));
                user.setAge(resultSet.getInt("age"));
                user.setTel(resultSet.getString("tel"));
                user.setAddress(resultSet.getString("address"));
                user.setJob(resultSet.getString("job"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(resultSet, pstmt, conn);
        }
        return user;
    }
    public boolean updateUserPassword(User user) {
        PreparedStatement pstmt = null;
        boolean result = false;
        ResultSet resultSet = null;
        // 创建数据库连接
        Connection conn = JdbcUtils.getConnection();
        // 更新数据库信息的SQL语句
        String sql = "UPDATE user SET password = ? WHERE username = ?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getPassword()); // 注意：在实际应用中，密码应该是加密存储的
            pstmt.setString(2, user.getUsername());
            int affectedRows = pstmt.executeUpdate();
            // 如果受影响的行数为1，则表示更新成功
            if (affectedRows == 1) {
                result = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(resultSet, pstmt, conn);
        }
        return result;
    }


    public List<User> getAllUsers() {
        PreparedStatement pstmt = null;
        List<User> users = new ArrayList<>();
        // 创建数据库连接
        Connection conn = DBConnectionUtil.getConn();
        // 插入数据库信息
        String sql = "select * from user";
        try {
            pstmt = conn.prepareStatement(sql);
            ResultSet resultSet = pstmt.executeQuery();
            while(resultSet.next()){
                User user = new User();
                user = new User();
                user.setId(resultSet.getString("id"));
                user.setUsername(resultSet.getString("username"));

                user.setGender(resultSet.getString("gender"));
                user.setAge(resultSet.getInt("age"));
                user.setTel(resultSet.getString("tel"));
                user.setAddress(resultSet.getString("address"));
                user.setJob(resultSet.getString("job"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConnectionUtil.close(pstmt, conn);
            return users;
        }
    }

    public void deleteFlightById(int id) {
        // 声明用到的对象
        PreparedStatement pstmt = null;
        // 创建数据库连接
        Connection conn = DBConnectionUtil.getConn();
        // 插入数据库信息
        String sql = "delete from user where id = ?";
        try {
            //声明对象
            pstmt = conn.prepareStatement(sql);
            //给占位符赋值
            pstmt.setInt(1, id);
            //执行sql语句
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DBConnectionUtil.close(pstmt, conn);
        }
    }

    public List<User> selectById(String id) {
        PreparedStatement pstmt = null;
        List<User> users = new ArrayList<>();
        // 创建数据库连接
        Connection conn = DBConnectionUtil.getConn();
        // 插入数据库信息
        String sql = "select * from user where id = ?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);
            ResultSet resultSet = pstmt.executeQuery();
            while(resultSet.next()){
                User user = new User();
                user = new User();
                user.setId(resultSet.getString("id"));
                user.setUsername(resultSet.getString("username"));

                user.setGender(resultSet.getString("gender"));
                user.setAge(resultSet.getInt("age"));
                user.setTel(resultSet.getString("tel"));
                user.setAddress(resultSet.getString("address"));
                user.setJob(resultSet.getString("job"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBConnectionUtil.close(pstmt, conn);
            return users;
        }
}}

